#!/bin/sh /etc/rc.common
# 初始化脚本：myservice
USE_PROCD=1          # 启用 procd 守护进程管理[1](@ref)
START=95             # 启动顺序（网络就绪后）
STOP=01              # 关闭顺序
CONFIGURATION=myservice  # 关联的 UCI 配置文件[1](@ref)

start_service() {
    # 加载 UCI 配置
    config_load "$CONFIGURATION"
    local name every
    config_get name hello name
    config_get every hello every

    # 启动服务实例
    procd_open_instance
    procd_set_param command /bin/sh "/var/myscript.sh" "$name" "$every"
    procd_set_param respawn \  # 崩溃自动重启[1](@ref)
        ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
    procd_set_param stdout 1    # 重定向日志到 syslog
    procd_set_param stderr 1
    procd_set_param file /etc/config/myservice  # 配置文件变更时自动重载
    procd_close_instance
}

stop_service() {
    # 自定义停止逻辑（若需清理资源）
    killall myscript.sh
}

reload_service() {
    # 配置重载时重启服务
    stop
    start
}

# 1. 安装脚本
chmod +x /etc/init.d/myservice
/etc/init.d/myservice enable

# 2. 创建配置文件
cat > /etc/config/myservice <<EOF
config hello
    option name 'Joost'
    option every '5'
EOF

# 3. 启动服务
/etc/init.d/myservice start